package com.timemetro.app;

import android.content.Context;
import android.content.res.XmlResourceParser;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.timemetro.app.stationObject.InfoNextStation;
import com.timemetro.app.stationObject.Station;
import com.timemetro.app.stationObject.TypeStation;

/**
 * Created by localadmin on 19.05.2015.
 */
public class ParsingXml{

    public ParsingXml() {
    }
    private static boolean isParsingHasBeen = false;
    private static Logger log = Logger.getLogger(ParsingXml.class.getName());
    public static   void Parse(Context context) throws IOException, XmlPullParserException {
        if (isParsingHasBeen) return;
        isParsingHasBeen = true;
        XmlResourceParser xrp =  context.getResources().getXml(R.xml.map);
        xrp.next();
        int eventType = xrp.getEventType();

        float positionX = 0;
        float positionY = 0;
        String name = "name";
        int branchId = 0;
        int stationId = 0;
        float timeUntilNextStation = 0;
        String nextNodes = "";
        String timeNextNode = "";
//       List<InfoNextStation> infoNextNodeList = new ArrayList<InfoNextStation>();
        TypeStation typeStation = TypeStation.USUAL;
        while (eventType != XmlPullParser.END_DOCUMENT) {
            List<InfoNextStation> infoNextNodeList = new ArrayList<InfoNextStation>();
            if (eventType == XmlPullParser.START_TAG && xrp.getName().equalsIgnoreCase("object")) {
                try {
                    positionX = xrp.getAttributeFloatValue(1, 0);
                }catch (Exception ex){// Если оказалось целое число
                    positionX = xrp.getAttributeIntValue(1, 0);
                }
                try {
                    positionY = xrp.getAttributeFloatValue(2, 0);
                }catch (Exception ex){// Если оказалось целое число
                    positionY = xrp.getAttributeIntValue(2, 0);
                }
                eventType = xrp.next();
            }
            if (eventType == XmlPullParser.START_TAG && xrp.getName().equalsIgnoreCase("property") && xrp.getAttributeValue(0).equalsIgnoreCase("branch-id")){
                try{
                branchId = xrp.getAttributeIntValue(1, 0);
                }catch (Exception ex){
                    branchId = 0;
                }
                eventType = xrp.next();
            }
            if (eventType == XmlPullParser.START_TAG && xrp.getName().equalsIgnoreCase("property") && xrp.getAttributeValue(0).equalsIgnoreCase("id")){
                try {
                    stationId = xrp.getAttributeIntValue(1, 0);
                } catch (Exception ex) {
                    stationId = 0;
                }
                eventType = xrp.next();
            }
            if (eventType == XmlPullParser.START_TAG && xrp.getName().equalsIgnoreCase("property") && xrp.getAttributeValue(0).equalsIgnoreCase("name")){
                name = xrp.getAttributeValue(1);
                eventType = xrp.next();
            }
            if (eventType == XmlPullParser.START_TAG && xrp.getName().equalsIgnoreCase("property") && xrp.getAttributeValue(0).equalsIgnoreCase("next_nodes")){
                nextNodes = xrp.getAttributeValue(1);
                eventType = xrp.next();
            }
            if (eventType == XmlPullParser.START_TAG && xrp.getName().equalsIgnoreCase("property") && xrp.getAttributeValue(0).equalsIgnoreCase("weight-node")){
                timeNextNode = xrp.getAttributeValue(1);
                eventType = xrp.next();
            }
            if (eventType == XmlPullParser.START_TAG && xrp.getName().equalsIgnoreCase("property") && xrp.getAttributeValue(0).equalsIgnoreCase("weight")){
                try {
                    timeUntilNextStation = xrp.getAttributeFloatValue(1, 0);
                }catch (Exception ex){// Если оказалось целое число
                    timeUntilNextStation = xrp.getAttributeIntValue(1, 0);
                }
               eventType = xrp.next();
            }
            if (eventType == XmlPullParser.START_TAG && xrp.getName().equalsIgnoreCase("property") && xrp.getAttributeValue(0).equalsIgnoreCase("position_name")){
                eventType = xrp.next();
            }
            if (eventType == XmlPullParser.START_TAG && xrp.getName().equalsIgnoreCase("property") && xrp.getAttributeValue(0).equalsIgnoreCase("type")){
                String s = xrp.getAttributeValue(1);
                if (s.equals("station")) {
                    typeStation = TypeStation.USUAL;

                } else if (s.equals("node")) {
                    typeStation = TypeStation.NODE;
                    try {
                        getNextNodeList(infoNextNodeList, nextNodes, timeNextNode);
                    } catch (Exception ex) {
                        log.log(Level.SEVERE, "Exception: ", ex);
                    }

                } else if (s.equals("node-ring")) {
                    typeStation = TypeStation.NODE_RING;
                    try {
                        if (name.equals("Краснопресненская")) {
                            int i = 0;
                        }
                        getNextNodeList(infoNextNodeList, nextNodes, timeNextNode);
                    } catch (Exception ex) {
                        log.log(Level.SEVERE, "Exception: ", ex);
                    }

                }
                ManagerStation.addStation(new Station(stationId, branchId, name, timeUntilNextStation, positionX, positionY, typeStation, infoNextNodeList));
                //eventType = xrp.next();
            }
            eventType = xrp.next();
        }
    }

    /*Для узловых станций установка id и времени пути до соседних узловых станций*/
    private static void getNextNodeList(List<InfoNextStation> infoNextStationList, String infoNextStation, String timeNextNode) {
        String[] idStations = infoNextStation.split(",");
        String[] timeStations = timeNextNode.split(",");
        if (idStations == null || timeStations == null) return;
        int id;
        float time;
        for (int i = 0; i < idStations.length; i++){
            id = Integer.valueOf(idStations[i]);
            time = Integer.valueOf(timeStations[i]);
            infoNextStationList.add(new InfoNextStation(id,time));
        }
    }
}
